﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace DrugStorehouseMIS.Comm
{
    public class Tool
    {
        TextBox textBox;
        DataGridView dataGridView;

        #region  将StatusStrip控件中的信息添加到treeView控件中
        /// <summary>
        /// 读取菜单中的信息.
        /// </summary>
        /// <param name="treeV">TreeView控件</param>
        /// <param name="MenuS">MenuStrip控件</param>
        public void GetMenu(TreeView treeV, MenuStrip MenuS)
        {
            for (int i = 0; i < MenuS.Items.Count; i++) //遍历MenuStrip组件中的一级菜单项
            {
                //将一级菜单项的名称添加到TreeView组件的根节点中，并设置当前节点的子节点newNode1
                TreeNode newNode1 = treeV.Nodes.Add(MenuS.Items[i].Text);
                //将当前菜单项的所有相关信息存入到ToolStripDropDownItem对象中
                ToolStripDropDownItem newmenu = (ToolStripDropDownItem)MenuS.Items[i];
                //判断当前菜单项中是否有二级菜单项
                if (newmenu.HasDropDownItems && newmenu.DropDownItems.Count > 0)
                    for (int j = 0; j < newmenu.DropDownItems.Count; j++)    //遍历二级菜单项
                    {
                        //将二级菜单名称添加到TreeView组件的子节点newNode1中，并设置当前节点的子节点newNode2
                        TreeNode newNode2 = newNode1.Nodes.Add(newmenu.DropDownItems[j].Text);
                        //将当前菜单项的所有相关信息存入到ToolStripDropDownItem对象中
                        ToolStripDropDownItem newmenu2 = (ToolStripDropDownItem)newmenu.DropDownItems[j];
                        //判断二级菜单项中是否有三级菜单项
                        if (newmenu2.HasDropDownItems && newmenu2.DropDownItems.Count > 0)
                            for (int p = 0; p < newmenu2.DropDownItems.Count; p++)    //遍历三级菜单项
                                //将三级菜单名称添加到TreeView组件的子节点newNode2中
                                newNode2.Nodes.Add(newmenu2.DropDownItems[p].Text);
                    }
            }
        }
        #endregion

        /// <summary>
        /// 产生各种单据
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GenerateNum(string type)
        {
            string number = type;
            DateTime date = DateTime.Now;
            string year = date.Year.ToString();
            string month = date.Month.ToString();
            if (month.Length < 2)
                month = "0" + month;
            string day = date.Day.ToString();
            if (day.Length < 2)
                day = "0" + day;
            string hour = date.Hour.ToString();
            if (hour.Length < 2)
                hour = "0" + hour;
            string minute = date.Minute.ToString();
            if (minute.Length < 2)
                minute = "0" + minute;
            string second = date.Second.ToString();
            if (second.Length < 2)
                second = "0" + second;

            number = number + year + month + day + hour + minute + second;
            return number;
        }

        public void BindDataTable(TextBox txt,DataGridView dgv,DataTable dt)
        {
            this.dataGridView = dgv;
            this.textBox = txt;

            this.dataGridView.Top = this.textBox.Top + this.textBox.Height;
            if (this.textBox.Text == "")
            {
                if (dt.DefaultView.Count != 0)
                    this.dataGridView.Visible = true;
            }
            this.dataGridView.DataSource = dt;

            dt.DefaultView.RowFilter = "'" + this.textBox.Text.Trim() + "%'";

            if (dt.DefaultView.Count != 0)
            {
                this.dataGridView.Visible = true;
                this.dataGridView.CurrentCell = this.dataGridView[0, 0];
            }
            else
                this.dataGridView.Visible = false;

            this.dataGridView.KeyDown += new KeyEventHandler(dataGridView_KeyDown);

            this.textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);

        }
    
        void dataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox.Text = this.dataGridView.CurrentCell.Value.ToString();
                this.dataGridView.Visible = false;
            }
        }

        void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (this.dataGridView.Rows.Count == 0)
                    return;

                this.dataGridView.Focus();
                if (this.dataGridView.CurrentRow.Index + 1 < this.dataGridView.Rows.Count)
                {
                    this.dataGridView.CurrentCell = this.dataGridView[0, this.dataGridView.CurrentRow.Index + 1];
                }
            }

            if (e.KeyCode == Keys.Enter)
            {
                this.textBox.Text = this.dataGridView.CurrentCell.Value.ToString();
                this.dataGridView.Visible = false;
            }
        }

        /// <summary>
        /// 使得文本框只允许输入数字
        /// </summary>
        /// <param name="txtEdit"></param>
        public static void OnlyNumber(TextBox txtEdit, char allow)
        {
            if (txtEdit.Text == "") return;
            char add_c = txtEdit.Text.Substring(txtEdit.Text.Length - 1, 1)[0];
            if (allow == add_c) return;
            if (add_c < '0' || add_c > '9')
            {
                txtEdit.Text = txtEdit.Text.Substring(0, txtEdit.Text.Length - 1);
                txtEdit.Select(txtEdit.Text.Length, 0);
            }
        }      

        /// <summary>
        /// 清空子控件文本
        /// </summary>
        /// <param name="obj"></param>
        public static void ResetAllTextBox(object obj)
        {

            System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
            for (int i = 0; i < control.Controls.Count; i++)
            {
                if (control.Controls[i] is TabControl || control.Controls[i] is GroupBox || control.Controls[i] is TabPage)
                    ResetAllTextBox(control.Controls[i]);

                if (control.Controls[i] is TextBox)
                {
                    control.Controls[i].Text = "";
                }
            }
        }
    }
}
